#include "tablemodels.h"


ObjectListModel::ObjectListModel(QObject *parent)
    : QAbstractTableModel(parent)
{
}

ObjectListModel::ObjectListModel(QList<CSLEPTuple> tuples, QObject *parent)
    : QAbstractTableModel(parent)
{
    listOfTuples=tuples;
}



int ObjectListModel::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);
    return listOfTuples.size();
}

int ObjectListModel::columnCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);
    return 3;
}


QVariant ObjectListModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
        return QVariant();
    
    if (index.row() >= listOfTuples.size() || index.row() < 0)
        return QVariant();
    
    if (role == Qt::DisplayRole) {
        CSLEPTuple tuple = (CSLEPTuple) listOfTuples.at(index.row());

        if (index.column() == 0)
			return tuple.objectName;
        else if (index.column() == 1)
			return tuple.objectInfo;
		else if (index.column() == 2)
			return tuple.filename;
		//else if (index.column() == 3 && tuple.type == SlepMatrix)
		//	return tuple.mtrx;
		//else if (index.column() == 3 && tuple.type == SlepVector)
		//	return tuple.vctr;
    }
    return QVariant();
}



QVariant ObjectListModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (role != Qt::DisplayRole)
        return QVariant();
    
    if (orientation == Qt::Horizontal) {
        switch (section) {
            case 0:
                return tr("Object Name");
                
            case 1:
                return tr("Object Info");

			case 2:
				return tr("File Name");
				
                
            default:
                return QVariant();
        }
    }
    return QVariant();
}



bool ObjectListModel::insertRows(int position, int rows, const QModelIndex &index)
{
    Q_UNUSED(index);
    beginInsertRows(QModelIndex(), position, position+rows-1);
    
    for (int row=0; row < rows; row++) {
		CSLEPTuple tuple;
		tuple.objectName = QString(" ");
		tuple.objectInfo = QString(" ");
		tuple.filename = QString(" ");
        listOfTuples.insert(position, tuple);
    }

    endInsertRows();
    return true;
}



bool ObjectListModel::removeRows(int position, int rows, const QModelIndex &index)
{
    Q_UNUSED(index);    
    beginRemoveRows(QModelIndex(), position, position+rows-1);
    
    for (int row=0; row < rows; ++row) {
        listOfTuples.removeAt(position);
    }

    endRemoveRows();
    return true;
}



bool ObjectListModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
	if (index.isValid() && role == Qt::EditRole) {
		int row = index.row();
				
		CSLEPTuple p = listOfTuples.value(row);
		
		if (index.column() == 0)
			p.objectName = value.toString();
		else if (index.column() == 1)
			p.objectInfo = value.toString();
		else if (index.column() == 2)
			p.filename= value.toString();
        else
            return false;
            
        listOfTuples.replace(row, p);
		emit(dataChanged(index, index));
		
        return true;
	}

	return false;
}



Qt::ItemFlags ObjectListModel::flags(const QModelIndex &index) const
{
    if (!index.isValid())
        return Qt::ItemIsEnabled;
    
    return QAbstractTableModel::flags(index) | Qt::ItemIsEditable;
}



QList<CSLEPTuple> ObjectListModel::getList()
{
    return listOfTuples;
}

